package com.vanysoft.business.example;

import com.vanysoft.business.base.org.dao.BaseOrgMapper;
import com.vanysoft.business.base.org.entity.TBaseOrg;
import com.vanysoft.common.utils.Excel.ExcelUtil;
import com.vanysoft.framework.exception.VanySoftBaseException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/example/excel")
public class ExampleExcelController {


    @Autowired
    private BaseOrgMapper baseOrgMapper;

    /**
     * Function Name               importExample
     *
     * @param file
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     * @throws VanySoftBaseException
     * @description 导入
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月2日 下午1:47:52           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/import", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String importExample(MultipartFile file) throws IOException, InvalidFormatException, VanySoftBaseException {
        //都能实现
        Workbook workbook = ExcelUtil.createByFactory(file);
//		Workbook workbook = ExcelUtil.createDifferentVersion(file);

        Sheet sheet = workbook.getSheetAt(0);

        for (Row row : sheet) {
            TBaseOrg org = new TBaseOrg();
            for (Cell cell : row) {
                switch (cell.getColumnIndex()) {
                    case 0:
                        org.setOrgCode(ExcelUtil.getCellValue(cell));
                        break;
                    case 1:
                        org.setOrgName(ExcelUtil.getCellValue(cell));
                        break;
                    case 2:
                        org.setParentOrgCode(ExcelUtil.getCellValue(cell));
                        break;
                    default:
                        break;
                }
                System.out.format("%-15s", ExcelUtil.getCellValue(cell));
            }
//			orgService.addOrg(org);
            System.out.println();
        }
        return "importExample";
    }

    @RequestMapping(value = "/export", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public void exportExample(HttpServletResponse response) throws InvalidFormatException, VanySoftBaseException {
        try {

            List<Map<String, Object>> sheetList = new ArrayList<Map<String, Object>>();
            Map<String, Object> sheet1 = new HashMap<String, Object>();

            String[] header1 = {"账号", "姓名", "性别", "部门", "状态"};
            String[] rowkey1 = {"userCode", "userName", "genderStr", "department", "statusStr"};

            sheet1.put("title", "第一个sheet页");
            sheet1.put("rows", baseOrgMapper.queryOrgUsers("961a78b5-d6e0-40a1-ae28-0e1e8ae26169", "", ""));
            sheet1.put("rowkey", rowkey1);
            sheet1.put("header", header1);
            sheetList.add(sheet1);

            Workbook workbook = ExcelUtil.exportExample(sheetList);
            String fileName = URLEncoder.encode("中文Excel", "UTF-8");

            // 清空response
            response.reset();
            // 设置response的Header
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            response.setContentType("text/html;charset=UTF-8");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            workbook.write(toClient);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
